<?php
class Novel extends MY_Controller
{

    protected $__enable_login_check = false;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('services/novel_service', 'novel_service');
    }

    public function getNovelDetail()
    {
        $novel_id = $this->request_array['novelId'] ?? null;
        $user_id = $this->_uid;
        $err = null;
        $detail = $this->novel_service->getNovelDetail($novel_id, $user_id, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK, $detail);
    }

    public function read()
    {
        $novel_id = $this->request_array['novelId'] ?? null;
        $order_num = $this->request_array['orderNum'] ?? 1;
        $is_pay = $this->request_array['isPay'] ?? 'N';
        $err = null;
        $read_content = $this->novel_service->getChapterContent($novel_id, $order_num, $this->_uid, $is_pay, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK, $read_content);
    }

    public function createNovel()
    {
        $novel_info = [
            'novel_name'        => $this->request_array['novelName'],
            'is_done'           => $this->request_array['isDone'],
            'author_id'         => $this->request_array['authorId'],
            'tag_id'            => $this->request_array['tagId'],
            'intro'             => $this->request_array['intro'],
            'cover_url'         => $this->request_array['coverUrl'] ?? null,
        ];
        $err = null;
        $res = $this->novel_service->createNovel($novel_info, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK);
    }

    public function getNovelChapters()
    {
        $novel_id = $this->request_array['novelId'] ?? null;
        $err = null;
        $res = $this->novel_service->getChaptersList($novel_id, $this->_uid, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK, $res);
    }

    public function getRandomRec()
    {
        $res = $this->novel_service->getRandomNovels();
        $this->renderJson(STATUS_OK, $res);
    }

    public function getChapterList()
    {
        $novel_id = $this->request_array['novelId'] ?? null;
        $err = null;
        $chapters = $this->novel_service->getChaptersList($novel_id, $this->_uid, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK, $chapters);
    }

    public function getReadLog()
    {
        $err = null;
        $read_log = $this->novel_service->getReadLog($this->_uid, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK, $read_log);
    }

    public function searchNovel()
    {
        $err = null;
        $search = $this->request_array['search'] ?? null;
        $search_res = $this->novel_service->searchNovels($search, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK, $search_res);
    }
}
